package com.xzd.util;


import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;


public class ExtHelper {
	
	public static JSONObject getJSONObject(Object object) throws Exception{
		JSONObject jsonObject = null;
		if(object != null){
				jsonObject = JSONObject.fromObject(object);
		}
		return jsonObject;
	}
	
	public static JSONObject getJSONObject(Object object, String datePattern) throws Exception{
		JSONObject jsonObject = null;
		
		if("".equals(datePattern) || datePattern == null) {
			datePattern = "yyyy-MM-dd HH:mm:ss";
		}
		
		//日期值处理器
		JsonConfig jsonConfig = configJson(null, datePattern);
		if(object != null){
				jsonObject = JSONObject.fromObject(object, jsonConfig);
		}
		
		return jsonObject;
	}
	
	public static String getJson(List beanList, String datePattern) {
		
		if("".equals(datePattern) || datePattern == null) {
			datePattern = "yyyy-MM-dd HH:mm:ss";
		}
		
		JSONObject jsonObject = new JSONObject();

		JsonConfig jsonConfig = configJson(null, datePattern);
		
		JSONArray array = JSONArray.fromObject(beanList,jsonConfig);
		
		jsonObject.accumulate("items", array);
		
		return jsonObject.toString();

	}
	
	public static JSONObject addTotal(long totalCount, JSONArray array) {
		
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.accumulate("totalCount", totalCount);
		
		jsonObject.accumulate("items", array);
		
		return jsonObject;
	}
	
	public static String getJsonFromJsonArray(JSONArray jsonArray) {
		
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.accumulate("items", jsonArray);
		
		return jsonObject.toString();
	}
	
	public static JSONArray getJsonFromList(List beanList,
			String datePattern) {
		
		if(datePattern == null || "".equals(datePattern)) {
			datePattern = "yyyy-MM-dd HH:mm:ss";
		}

		JsonConfig jsonConfig = configJson(null, datePattern);

		JSONArray array = JSONArray.fromObject(beanList,jsonConfig);

		return array;
	}
	
	/**
	 * 将一个实体类转换成EXT表格需要JsonMapping
	 * @param c
	 * @return
	 */
	public static String getExtJsDataFiledsForJson(Class c) {
		Field[] fields = c.getDeclaredFields();
		JSONArray jsonArray = new JSONArray();
		for(int i=0; i<fields.length; i++) {
			JSONObject jsonObj = new JSONObject();
			jsonObj.accumulate("name", fields[i].getName());
			if( fields[i].getType().getSimpleName().toLowerCase().equals("long") ){
				jsonObj.accumulate("type", "int");
			}else if(fields[i].getType().getSimpleName().toLowerCase().equals("date")){
				jsonObj.accumulate("type", "string");
			}else{
				jsonObj.accumulate("type", fields[i].getType().getSimpleName().toLowerCase());
			}
			jsonObj.accumulate("mapping", fields[i].getName());
			jsonArray.add(jsonObj);
		}
		
		return jsonArray.toString();
	}

	public static JsonConfig configJson(String[] excludes, String datePattern) {
		JsonConfig jsonConfig = new JsonConfig();
		if (excludes != null) {
			jsonConfig.setExcludes(excludes);
		}
		jsonConfig.setIgnoreDefaultExcludes(false);
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jsonConfig.registerJsonValueProcessor(Date.class,
				new DateJsonValueProcessor(datePattern));

		return jsonConfig;
	}
}

class DateJsonValueProcessor implements JsonValueProcessor {
	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	private DateFormat dateFormat;

	/**
	 * 构造方法.
	 * 
	 * @param datePattern
	 *            日期格式
	 */
	public DateJsonValueProcessor(String datePattern) {
		try {
			dateFormat = new SimpleDateFormat(datePattern);
		} catch (Exception ex) {
			dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		}
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {
		
		if(value == null) {
			return "";
		}
		
		return dateFormat.format((Date) value);
	}
	
}
